/*************************************************************************
	> File Name: thread.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/10/04 17:44
 ************************************************************************/

#pragma once

#include "log.hpp"
#include <pthread.h>

typedef void*(*fun_t)(void*);

const int SIZE = 32;

class ThreadData
{
public:
    void* _args;
    std::string _name;
};

class Thread
{
public:
    Thread (const int num, fun_t callback, void* args):_func(callback), _tdata(new ThreadData())
    {
        _tdata->_name = "Thread-" + std::to_string(num);
        _tdata->_args = args;
    }

    void start()
    {
        pthread_create(&_tid, nullptr, _func, _tdata);
    }

    void join() { 
        pthread_join(_tid, nullptr);
    }

    std::string name() const
    {
        return _tdata->_name;
    }

private:
    pthread_t _tid;
    ThreadData* _tdata;
    fun_t _func;
};
